#include "recorder_fun.h"
#include <process.h>

static int work;
static int success;
static int sc_x, sc_y;
extern int qmax;
extern int sc_width;
extern int sc_height;
extern int recording;
extern clock_t start;

const static char filename[100] = "temp.avi";
static const char file_comment[] = "This file is written down with Shotscreens program, http://shotscreens.appspot.com/";

void start_video_recorder_th(void* ch)
{
    AVOutputFormat *fmt;
    AVFormatContext *oc;
    AVStream *video_st;
    uint8_t* frame = NULL;
    int i;

    ClearError();
    success = 0;
    work = 1;

    if (sc_width < 8 || sc_height < 8 || (sc_height % 2))
    {
        SetError("The measurement is not correctly given!");
        goto end;
    }
    register_mpeg();
    // detect the output format
    fmt = guess_format("avi", NULL, NULL);
    if (!fmt) {
        SetError("Could not find suitable output format");
        goto end;
    }
    // allocate the output media context
    oc = avformat_alloc_context();
    if (!oc) {
        SetError("Memory error");
        goto end;
    }
    oc->oformat = fmt;
    snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
    snprintf(oc->comment, sizeof(oc->comment), "%s", file_comment);
    // add the video stream using the default format codecs
    // and initialize the codecs
    video_st = NULL;
    if (fmt->video_codec != CODEC_ID_NONE) {
        video_st = add_video_stream(oc, fmt->video_codec);
    }

    // set the output parameters (must be done even if no
    // parameters).
    if (av_set_parameters(oc, NULL) < 0) {
        SetError("Invalid output format parameters");
        goto clear_st;
    }

    // now that all the parameters are set, we can open the video
    // codec and allocate the necessary encode buffers
    if (video_st)
        open_video(oc, video_st);
    // open the output file, if needed
    if (!(fmt->flags & AVFMT_NOFILE)) {
        if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
            SetError("Could not open temp file");
            goto clear_video;
        }
    }

    // write the stream header, if any
    av_write_header(oc);
    start = clock();
    recording = 1;
    frame = (uint8_t*) malloc(sc_height*lineSizeOfFrame(sc_width));
    for(;;) {
        if (!video_st || !recording)
            break;
        // write interleaved video frame
        if (getScreenshotWithCursor(frame, sc_x, sc_y, sc_width, sc_height))
            write_video_frame(oc, video_st, frame);
    }
    free(frame);
    // write the trailer, if any.  the trailer must be written
    // before you close the CodecContexts open when you wrote the
    // header; otherwise write_trailer may try to use memory that
    // was freed on av_codec_close()
    av_write_trailer(oc);

    if (!(fmt->flags & AVFMT_NOFILE)) {
        // close the output file
        url_fclose(oc->pb);
    }
    success = 1;

clear_video:
    // close each codec
    if (video_st)
        close_video(oc, video_st);

clear_st:
    // free the streams
    for(i = 0; i < oc->nb_streams; i++) {
        av_freep(&oc->streams[i]->codec);
        av_freep(&oc->streams[i]);
    }

clear_oc:
    // free the stream
    av_free(oc);

end:
    work = 0;
    _endthread();
    return;
}

int start_video_recorder(int x, int y, int width, int height)
{
    sc_x = x;
    sc_y = y;
    sc_width = width;
    sc_height = height + height % 2;
    _beginthread(start_video_recorder_th, 0, NULL);
}

int isSuccessful()
{
    return success;
}

int isWork()
{
    return work;
}

void stop_video_recoder()
{
    recording = 0;
}

int main()
{
    return 0;
}
